﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#ifndef __SERVER_LOADER_H__
#define __SERVER_LOADER_H__

#pragma once

#include "singleton.h"

#include "commonpacket.h"
#include "CommonServer.h"

#include "CommonDlg.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 服务加载器子系统加载器
class CServerLoader : public MObject
{
    DECLARE_CLASS_LOADER( CServerLoader, XUI )
    DECLARE_CLASS_LOADER( CServerLoader, NET )
    DECLARE_CLASS_LOADER( CServerLoader, Center )
    DECLARE_CLASS_LOADER( CServerLoader, Balance )
    DECLARE_CLASS_LOADER( CServerLoader, AuthorDB )
    DECLARE_CLASS_LOADER( CServerLoader, Author )
    DECLARE_CLASS_LOADER( CServerLoader, MasterDB )
    DECLARE_CLASS_LOADER( CServerLoader, Master )
    DECLARE_CLASS_LOADER( CServerLoader, Service )
    DECLARE_CLASS_LOADER( CServerLoader, Gate )
#if (__PLATFORM_TARGET__ == PLATFORM_TARGET_WINDOWS)
public:
    static Int ThreadExceptHandler(TId tThreadId, Int nThreadType, Int nThreadRoutine, void* pRoutine, struct _EXCEPTION_POINTERS* pExceptInfo);
#endif
public:
    CServerLoader(void);
    ~CServerLoader(void);

    bool Init(PCXStr pszCmd = nullptr);
    bool Init(Int nArgc, PXStr* ppszArgv);
    void Exit(void);

    bool LoadServer(Int nServer, CEventHandler* pSHandler);
    void UnloadServers(void);

    bool StartServers(void);
    void StopServers(void);
private:
    bool InitSystems(void);
    void ExitSystems(void);
public:
    CServerConfig       m_Config;

    CXUIManagerPtr      m_UIManagerPtr;
    CNetworkPtr         m_NetworkPtr;

    CCommonServerPtr    m_CenterServer;
    CCommonServerPtr    m_BalanceServer;
    CCommonServerPtr    m_AuthorDBServer;
    CCommonServerPtr    m_AuthorServer;
    CCommonServerPtr    m_MasterDBServer;
    CCommonServerPtr    m_MasterServer;
    CCommonServerPtr    m_ServiceServer;
    CCommonServerPtr    m_GateServer;
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 服务加载器子系统加载器单件
class CServerLoaderSingleton : public CTSingletonInst<CServerLoader>
{
//public:
//    CServerLoaderSingleton(void);
//    ~CServerLoaderSingleton(void);
//    CServerLoaderSingleton(const CServerLoaderSingleton&);
//    CServerLoaderSingleton& operator=(const CServerLoaderSingleton&);
};
typedef CServerLoaderSingleton GServerLoader;
#define GServerLoaderInst      CServerLoaderSingleton::GetInstance()

#endif // __SERVER_LOADER_H__
